package org.tote.util;

import java.util.UUID;

import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;

/**
 * Generates random token by generating random string and converts it to MD5 hash.
 * 
 */
public class TokenGenerator {

	/**
	 * Converts input string to MD5 hash code
	 * 
	 * @param string
	 *            - string to convert
	 * @return - MD5 hash code string
	 */
	public static String convertToMD5(String string) {
		MessageDigestPasswordEncoder passwordEncoder = new MessageDigestPasswordEncoder("MD5");
		return passwordEncoder.encodePassword(string, null);
	}

	/**
	 * Converts input string to SHA-256 hash code
	 * 
	 * @param string
	 *            - string to convert
	 * @return - SHA-256 hash code string
	 */
	public static String convertToSHA256(String string) {
		MessageDigestPasswordEncoder passwordEncoder = new MessageDigestPasswordEncoder("SHA-256");
		return passwordEncoder.encodePassword(string, null);
	}

	public static String generateRandomToken() {
		return convertToMD5(UUID.randomUUID().toString());
	}
}
